﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows;
using System.Windows.Media;
using Fireball.Extensions;
using System.Windows.Markup;
using System.ComponentModel;

namespace Fireball.OpenXML.Word
{
    /// <summary>
    /// The run is the implementation for Inline, and it can contains,
    /// text picture and other content
    /// </summary>
    [ContentProperty("Content")]
    public class Run : Inline, IFindable
    {
        private RunContent _RunContent = null;

        public Run()
        {
            /*this.FontFamily = new FontFamily("Calibri");
            this.FontSize = 11;
            this.TextDecoration = TextDecoration.None;*/
        }

        public Run(RunContent runContent)
            : this()
        {
            _RunContent = runContent;
        }

        public Run(string cText)
            : this()
        {
            _RunContent = new RunContentText(cText);
        }

        /// <summary>
        /// The font family for the text of this
        /// run
        /// </summary>
        public FontFamily FontFamily
        {
            get;
            set;
        }

        /// <summary>
        /// The text decoration for the font on this run
        /// </summary>
        [DefaultValue(TextDecoration.None)]
        public TextDecoration TextDecoration
        {
            get;
            set;
        }

        /// <summary>
        /// Size of the font,specified in Points, they will be converted automatically in Half Points (Points * 2)
        /// </summary>
        public int? FontSize
        {
            get;
            set;
        }
   

        /// <summary>
        /// The content for this Run
        /// </summary>
        public RunContent Content
        {
            get
            {
                return _RunContent;
            }
            set
            {
                _RunContent = value;
            }
        }

        /// <summary>
        /// The Foreground Color for this Run
        /// </summary>
        public Color Foreground
        {
            get;
            set;
        }

        public override System.Xml.Linq.XContainer ToXml()
        {
            RunContent content = this.Content;
            if (content == null) content = new RunContentText("");

            XElement xRun = new XElement(NS.WM + "r", content.ToXml());

            XElement xRunStyle = new XElement(NS.WM + "rPr");

            //XElement fontStyle = null;

            if (this.FontFamily != null)
            {
                string fontName = this.FontFamily.FamilyNames.First().Value;
                XElement xFonts = new XElement(NS.WM + "rFonts",
                    new XAttribute(NS.WM + "ascii", fontName),
                    new XAttribute(NS.WM + "hAnsi", fontName));

                xRunStyle.Add(xFonts);
            }

if (this.FontWeight == FontWeights.Bold)
            {
                var fontStyle = new XElement(NS.WM + "b");

                xRunStyle.Add(fontStyle);
            }
            else if( this.FontWeight != FontWeights.Normal)
            {
                throw new NotImplementedException();
            }

            if (this.FontStyle == FontStyles.Italic)
            {
               var fontStyle = new XElement(NS.WM + "i");

               xRunStyle.Add(fontStyle);
            }
            else if(this.FontStyle != FontStyles.Normal)
            {
                throw new NotImplementedException();
                //fontStyle = new XElement(NS.WM + "i");
            }

            if (this.Foreground != null)
            {
                string sf = this.Foreground.ToHexFormat();

                xRunStyle.Add(new XElement(NS.WM + "color",
                    new XAttribute(NS.WM + "val", sf)));
            }

            if (this.FontSize != null)
            {
                XElement xSize = new XElement(NS.WM + "sz",
                    new XAttribute(NS.WM + "val", this.FontSize.Value * 2));

                xRunStyle.Add(xSize);
            }



            if (this.TextDecoration !=  TextDecoration.None)
            {
                if (this.TextDecoration == TextDecoration.Underline)
                {
                    xRunStyle.AddFirst(new XElement(NS.WM + "u",
                        new XAttribute(NS.WM + "val","single")));
                }
                else if (this.TextDecoration == TextDecoration.Baseline)
                {
                    xRunStyle.Add(new XElement(NS.WM
                        + "vertAlign", new XAttribute(NS.WM + "val", "baseline")));
                }
                else if (this.TextDecoration == TextDecoration.Superscript)
                {
                    xRunStyle.Add(new XElement(NS.WM
                        + "vertAlign", new XAttribute(NS.WM + "val", "superscript")));
                }
                else if (this.TextDecoration == TextDecoration.Subscript)
                {
                    xRunStyle.Add(new XElement(NS.WM
                        + "vertAlign", new XAttribute(NS.WM + "val", "subscript")));
                }
            }

            /*if (fontStyle != null)
            {
                xRunStyle.Add(fontStyle);
                fontStyle = null;
            }

            

            if (fontStyle != null)
            {
                xRunStyle.Add(fontStyle);
                fontStyle = null;
            }*/

            xRun.AddFirst(xRunStyle);

            return xRun;
        }

        #region IFindable Members

        public OpenXMLEntity FindByName(string name)
        {
            if (this.Content != null)
                if (this.Content.Name == name)
                    return this.Content;
            return null;
        }

        #endregion
    }
}
